/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pruebaciudad.sopa;

/**
 *
 * @author Pucci
 */
public class HiloBuscador extends Thread {
    private char[][] tablero_sec;
    private String palabra;
    
    public HiloBuscador (char[][] tablero, String palabra){
        this.tablero_sec = tablero;
        this.palabra = palabra.toUpperCase();
    }
    
    public void run(){
        boolean encontrado = false;
        for (int i = 0; i < 4; i++){
            if (revisar()){
                encontrado = true;
                break;
            }
            rotarTablero();
        }
        
        if (encontrado){
            System.out.println(System.currentTimeMillis() + ": " + palabra);
        } else {
            System.err.println("No se encontró la palabra " + palabra);
        }
    }

    private boolean revisar() {
        for (char[] fila : tablero_sec){
            if (new String(fila).contains(palabra)){
                return true;
            }
        }
        return false;
    }

    private void rotarTablero() {
        char[][] tablero_temp = new char[tablero_sec[0].length][tablero_sec.length];
        
        for (int i = 0, n = tablero_sec.length; i < n; i++){
            for (int j = 0, m = tablero_sec[0].length; j < m; j++){
                tablero_temp[j][n - i - 1] = tablero_sec[i][j];
            }
        }
        
        tablero_sec = tablero_temp;
    }
    
}
